//
// Description: 129. 求根节点到叶节点数字之和
// Created by Loading on 2021/10/28.
//

#include <bits/stdc++.h>
#include <Data_Structure_Definition.h>

using namespace std;

int sumNumbers(TreeNode *root) {
    int sum = 0;

    function<void(TreeNode *, int)> dfs = [&](TreeNode *node, int val) {
        if (!node) {
            return;
        }
        // 到叶子节点，累加
        if (!node->left && !node->right) {
            sum += val + node->val;
            return;
        }

        dfs(node->left, (val + node->val) * 10);
        dfs(node->right, (val + node->val) * 10);
    };

    dfs(root, 0);

    return sum;
}

int main() {
    auto *root = new TreeNode(1);
    root->left = new TreeNode(2);
    root->right = new TreeNode(3);

    int res = sumNumbers(root);
    cout << res << endl;

    return 0;
}